519C - A and B and Team Training - CodeForces Solution


greedy implementation math number theory *1300

Please click on ads to support us..

Python Code:

n,m=map(int,input().split())
ans=0
while (m>=2 and n>=1)or (m>=1 and n>=2) :
    ans+=1
    if(m>n):
        m-=2
        n-=1
    else:
        m-=1
        n-=2
print(ans)
    

C++ Code:

#include<bits/stdc++.h>
/*
author:- laksheya_0809
*/
using namespace std;

#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define pb push_back
#define ppb pop_back
#define vvi vector<vi>
#define vii vector<ii>
#define ln << '\n'
#define sz(x) (int)((x).size())
#define _for(s, n, upd) for(int i = s; i < n; i+=upd)
#define rep(i, s, n) for(int i = s; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define fill(x, y) memset(x, y, sizeof x) 
#define ump unordered_map
const int MOD = 1e9+7;

void sol() {
    int n, m; cin >> n >> m;
    int teams = 0;
    while(n > 0 && m > 0) {
        if(n > m) {
            n -= 2;
            m -= 1;
        } else {
            m -= 2;
            n -= 1;
        }
        if(n >= 0 && m >= 0)
            teams++;
    }
    cout << teams ln;
}



signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while(t--) sol();
    return 0;
}


Comments

Submit
0 Comments
More Questions

429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array